<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 国内商标
 */
class Trademark extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
        $this->load->model("trademark_model");
        $this->load->model("client_model");
        $this->load->model("user_model");
        $this->load->model("user_role_model");
        $this->load->model("partner_model");
        $this->load->model("contact_model");
        $this->load->model("reminder_model");
        $this->load->model("document_model");
        $this->load->helper('form');
        $this->load->helper('string');
        $this->load->library('form_validation');
    }
    
    /**
     * 国内商标列表
     * @param type $page
     */
    public function index($page = 1) {
        $conditions = $this->_get_search_conditions();

        $this->config->load('pagination', true);
        $pagination = $this->config->item('pagination');
        $pagination['base_url'] = site_url('trademark/index');
        $pagination['total_rows'] = $this->trademark_model->fetch_count($conditions);
        $this->load->library('pagination', $pagination);
        
        $data = array(
            'title' => '国内商标档案 - 档案列表',
            'operations' => array(
                array('label' => '添加档案', 'uri' => 'trademark/create'),
            ),
            'conditions' => $conditions,
            'trademarks' => $this->trademark_model->fetch_array($conditions, $pagination['per_page'], $page),
            'pagelink' => $this->pagination->create_links()
        );
        $data['case_types'] = array('' => '') + $this->_case_type_options();
        $data['agents'] = $this->_user_realname_options();
        $data['partners'] = $this->_partner_name_options();

		$this->load->view('header', $data);
        $this->load->view('trademark/list', $data);
        $this->load->view('footer');
	}
    
    /**
     * 导出
     */
    public function export() {
        $conditions = $this->_get_search_conditions();

        $this->load->dbutil();
        $query = $this->trademark_model->export_query($conditions);

        $data = $this->dbutil->csv_from_result($query);
        
        header('Content-Type: "text/x-comma-separated-values"');
        header('Content-Disposition: attachment; filename="trademarks.csv"');
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Pragma: no-cache');
        header("Content-Length: ".strlen($data));
        exit($data);
    }


    /**
     * 添加国内商标
     */
    public function create() {
        
        $data = array(
            'title' => '国内商标档案 - 添加档案',
            'operations' => array(
                array('label' => '档案列表', 'uri' => 'trademark')
            )
        );
        
        if (count($_POST) > 0)
		{
            /* 有提交数据 */
            $reminders = $this->_get_form_reminders();
            $documents = $this->_get_form_documents();
            
            $this->_set_form_validation_rules();
            if ($this->form_validation->run()) {
                /* 插入数据 */
                $trademark = $this->_get_form_trademark();
                $this->trademark_model->insert($trademark);
                $trademark['file_no'] = $this->trademark_model->file_no();
                $tm_id = $this->trademark_model->insert_id();
                
                foreach ($reminders as $reminder) {
                    $reminder['relation_id'] = RM_R_TRADEMARK . $tm_id;
                    $reminder['relation_file_no'] = $trademark['file_no'];
                    $reminder['relation_c_name'] = $trademark['c_name'];
                    $this->reminder_model->insert($reminder);
                }
                
                foreach ($documents as $doc) {
                    $doc['relation_id'] = DOC_R_TRADEMARK . $tm_id;
                    $this->document_model->insert($doc);
                }
                
                $data['sucess_message'] = '您已成功添加了档案 ' . $trademark['file_no'];
                $data['operations'][] = array('label' => '再次添加档案', 'uri' => 'trademark/create');
                $this->load->view('header', $data);
                $this->load->view('footer');
                
                return;
            }
		} else {
            $reminders = array();
            $documents = array();
        }

        /* 显示添加表单 */
        $data['action'] = 'trademark/create';
        $data['trademark'] = $this->trademark_model->new_trademark();
        $data['case_types'] = $this->_case_type_options();
        $data['client_options'] = $this->_client_options();
        $data['agencies'] = $this->_agency_options();
        $data['user_options'] = $this->_active_user_options();
        $data['partner_options'] = $this->_partner_options();
        $data['financial_users'] = $this->_financial_user_options();
        $data['empty_reminder_html'] = $this->_get_empty_reminder_html();
        $data['reminders_html'] = $this->_get_reminders_html($reminders);
        $data['empty_document_html'] = $this->_get_empty_document_html();
        $data['documents_html'] = $this->_get_documents_html($documents);
        
        $case_type = set_value('case_type', CT_ZCSQ);
        $data['dynamic_title'] = $this->_get_dynamic_title($case_type);
        $data['dynamic_html'] = $this->_get_dynamic_html($case_type);
        $this->load->view('header', $data);
        $this->load->view('trademark/form', $data);
        $this->load->view('footer');
    }
    
    /**
     * 修改档案
     * @param type $id
     */
    public function update($id) {
        $trademark = $this->trademark_model->get($id);
        if (empty($trademark)) {
            /* 档案不存在 */
            show_404();
        }

        $data = array(
            'title' => '国内商标档案 - 修改档案 - ' . $trademark['file_no'],
            'operations' => array(
                array('label' => '档案列表', 'uri' => 'trademark')
            )
        );
        
        if (count($_POST) > 0)
		{
            /* 有提交数据 */
            $reminders = $this->_get_form_reminders();
            $documents = $this->_get_form_documents();

            $this->_set_form_validation_rules();
            if ($this->form_validation->run()) {
                /* 验证通过，更新数据 */
                $trademark = $this->_get_form_trademark();
                $this->trademark_model->update($id, $trademark);

                $relation_id = RM_R_TRADEMARK . $id;
                $this->reminder_model->delete_batch_by_relation($relation_id);
                foreach ($reminders as $reminder) {
                    $reminder['relation_id'] = $relation_id;
                    $reminder['relation_file_no'] = $trademark['file_no'];
                    $reminder['relation_c_name'] = $trademark['c_name'];
                    $this->reminder_model->insert($reminder);
                }
                
                $relation_id = DOC_R_TRADEMARK . $id;
                $this->document_model->delete_batch_by_relation($relation_id);
                foreach ($documents as $doc) {
                    $doc['relation_id'] = $relation_id;
                    $this->document_model->insert($doc);
                }
                
                $data['sucess_message'] = '您已成功修改了档案 ' . $trademark['file_no'];
                $data['operations'][] = array('label' => '再次修改档案', 'uri' => 'trademark/update/' . $id);
                $this->load->view('header', $data);
                $this->load->view('footer');
                
                return;
            }
		} else {
            /* 没有提交数据 */
            $reminders = $this->reminder_model->fetch_array(array('relation_id' => RM_R_TRADEMARK . $id));
            $documents = $this->document_model->fetch_array(array('relation_id' => DOC_R_TRADEMARK . $id));
        }
        
        /* 显示修改表单 */
        $data['action'] = 'trademark/update/' . $id;
        $data['trademark'] = $trademark;
        $data['case_types'] = $this->_case_type_options();
        $data['client_options'] = $this->_client_options();
        $data['agencies'] = $this->_agency_options();
        $data['user_options'] = $this->_active_user_options();
        $data['partner_options'] = $this->_partner_options();
        $data['financial_users'] = $this->_financial_user_options();
        $data['empty_reminder_html'] = $this->_get_empty_reminder_html();
        $data['reminders_html'] = $this->_get_reminders_html($reminders);
        $data['empty_document_html'] = $this->_get_empty_document_html();
        $data['documents_html'] = $this->_get_documents_html($documents);
        
        $case_type = set_value('case_type', $trademark['case_type']);
        $data['dynamic_title'] = $this->_get_dynamic_title($case_type);
        $data['dynamic_html'] = $this->_get_dynamic_html($case_type, $trademark);
        $this->load->view('header', $data);
        $this->load->view('trademark/form', $data);
        $this->load->view('footer');
    }
    
    /**
     * 查看档案
     * @param type $id
     */
    public function view($id) {
        $trademark = $this->trademark_model->get($id);
        if (empty($trademark)) {
            /* 档案不存在 */
            show_404();
        }

        $data = array(
            'title' => '国内商标档案 - 查看档案 - ' . $trademark['file_no'],
            'operations' => array(
                array('label' => '档案列表', 'uri' => 'trademark')
            )
        );
        
        $reminders = $this->reminder_model->fetch_array(array('relation_id' => RM_R_TRADEMARK . $id));
        $documents = $this->document_model->fetch_array(array('relation_id' => DOC_R_TRADEMARK . $id));
        
        /* 显示修改表单 */
        $data['tm'] = $trademark;
        $data['rms'] = $reminders;
        $data['docs'] = $documents;
        $data['dynamic_view'] = $this->_get_dynamic_view();
        $this->load->view('header', $data);
        $this->load->view('trademark/view', $data);
        $this->load->view('footer');
    }
    
    /**
     * 删除档案
     * @param type $id
     */
    public function remove($id) {
        $trademark = $this->trademark_model->get($id);
        if (empty($trademark)) {
            /* 档案不存在 */
            show_404();
        }
        
        $data = array(
            'title' => '国内商标档案 - 删除档案 - ' . $trademark['file_no'],
            'operations' => array(
                array('label' => '档案列表', 'uri' => 'trademark')
            )
        );
        
        $this->trademark_model->delete($id);
                
        $data['sucess_message'] = '您已成功删除了档案 ' . $trademark['file_no'];
        $this->load->view('header', $data);
        $this->load->view('footer');

    }
    
    /**
     * ajax取动态信息
     */
    public function get_dynamic_info() {
        $case_type = $this->input->get('case_type');
        $data = array(
            'title' => $this->_get_dynamic_title($case_type),
            'html'  => $this->_get_dynamic_html($case_type),
        );
        echo json_encode($data);
    }


    /**
     * ajax 取得申请人信息和联系人信息
     */
    public function get_client() {
        $c_id = $this->input->get('c_id');
        $client = $this->client_model->get($c_id);
        $contact = array();
        if (!empty($client)) {
            $contacts = $this->contact_model->fetch_array(array('ids' => $client['contact_ids']));
            if (count($contacts) > 0) {
                $contact = $contacts[0];
            }
        }
        echo json_encode(array('client' => $client, 'contact' => $contact));
    }
    
    /**
     * ajax 取得合作所信息和联系人信息
     */
    public function get_partner() {
        $p_id = $this->input->get('p_id');
        $partner = $this->partner_model->get($p_id);
        $contact = array();
        if (!empty($partner)) {
            $contacts = $this->contact_model->fetch_array(array('ids' => $partner['contact_ids']));
            if (count($contacts) > 0) {
                $contact = $contacts[0];
            }
        }
        echo json_encode(array('partner' => $partner, 'contact' => $contact));
    }
    
    /**
     * 取动态页面标题
     * @param string $case_type 案件类型
     * @return string
     */
    private function _get_dynamic_title($case_type = CT_ZCSQ) {
        switch ($case_type) {
            case CT_ZCSQ:
                $title = '申请';
                break;
            case CT_ZRSQ:
                $title = '转让';
                break;
            case CT_BGSQ:
                $title = '变更';
                break;
            case CT_XZSQ:
                $title = '续展';
                break;
            case CT_BFZCZ:
            case CT_CHSQ:
            case CT_GZSQ:
            case CT_TGYXQ:
            case CT_TGZCZM:
            case CT_XKBA:
            case CT_QTSQ:
                $title = '其他申请';
                break;
            default :
                $title = '案件';
                break;
        }
        
        return $title;
    }
    
    /**
     * 取动态页面view
     * @param string $case_type 案件类型
     * @return string
     */
    private function _get_dynamic_view($case_type = CT_ZCSQ) {
        switch ($case_type) {
            case CT_ZCSQ:
                $view = CTV_APPLICATION;
                break;
            case CT_ZRSQ:
                $view = CTV_TRANSFER;
                break;
            case CT_BGSQ:
                $view = CTV_CHANGE;
                break;
            case CT_XZSQ:
                $view = CTV_RENEWAL;
                break;
            case CT_BFZCZ:
            case CT_CHSQ:
            case CT_GZSQ:
            case CT_TGYXQ:
            case CT_TGZCZM:
            case CT_XKBA:
            case CT_QTSQ:
                $view = CTV_OTHER;
                break;
            default :
                $view = CTV_CASE;
                break;
        }
        
        return $view;
    }

    /**
     * 取动态页面内容
     * @param string $case_type 案件类型
     * @param array $trademark 商标档案信息
     * @return string
     */
    private function _get_dynamic_html($case_type = CT_ZCSQ, $trademark = array()) {
        $view = $this->_get_dynamic_view($case_type);
        
        return trim_line_break($this->load->view('trademark/' . $view, array('trademark' => $trademark), true));
    }

    /**
     * 取限时提醒html
     * @return string
     */
    private function _get_empty_reminder_html() {
        return trim_line_break($this->load->view('trademark/reminder', '', true));
    }
    private function _get_reminders_html($reminders) {
        $html = '';
        foreach ($reminders as $reminder) {
            $html .= trim_line_break($this->load->view('trademark/reminder', array('reminder' => $reminder), true));
        }

        return $html;
    }
    
    /**
     * 取官方文件html
     * @return string
     */
    private function _get_empty_document_html() {
        return trim_line_break($this->load->view('trademark/document', '', true));
    }
    private function _get_documents_html($documents) {
        $html = '';
        foreach ($documents as $document) {
            $html .= trim_line_break($this->load->view('trademark/document', array('document' => $document), true));
        }

        return $html;
    }
    
    /**
     * 取搜索条件
     * @return type
     */
    private function _get_search_conditions() {
        $in = $this->input;
        return array(
            'c_name' => trim($in->post('c_name')),
            'application_no' => trim($in->post('application_no')),
            'tm_name' => trim($in->post('tm_name')),
            'case_type' => ($in->post('case_type') !== false) ? $in->post('case_type') : '',
            'agent' => ($in->post('agent') !== false) ? $in->post('agent') : '',
            'partner' => ($in->post('partner') !== false) ? $in->post('partner') : '',
            'file_create_date_from' => trim($in->post('file_create_date_from')),
            'file_create_date_to' => trim($in->post('file_create_date_to')),
        );

    }


    /**
     * 从表单取档案信息
     * @return array
     */
    private function _get_form_trademark() {
        $in = $this->input;
        
        $trademark = array(
            /* 基本信息 */
            'file_no' => $in->post('file_no'),
            'file_create_date' => $in->post('file_create_date'),
            'case_type' => $in->post('case_type'),
            'c_name' => $in->post('c_name'),
            'c_name_en' => $in->post('c_name_en'),
            'c_addr' => $in->post('c_addr'),
            'c_addr_en' => $in->post('c_addr_en'),
            'c_licence_number' => $in->post('c_licence_number'),
            'c_country' => $in->post('c_country'),
            'other_client_info' => $in->post('other_client_info'),
            'agency' => $in->post('agency'),
            'agent' => $in->post('agent'),
            'partner' => $in->post('partner'),
            'ct_contact' => $in->post('ct_contact'),
            'ct_telephone' => $in->post('ct_telephone'),
            'ct_fax' => $in->post('ct_fax'),
            'ct_email' => $in->post('ct_email'),
            'ct_qq' => $in->post('ct_qq'),
            'ct_mailing_addr' => $in->post('ct_mailing_addr'),
            
            /* 费用信息 */
            'government_fee' => $in->post('government_fee'),
            'agency_fee' => $in->post('agency_fee'),
            'is_collected' => ($in->post('is_collected') !== false) ? "是" : "否",
            'collection_date' => $in->post('collection_date'),
            'collection_account' => $in->post('collection_account'),
            'is_billed' => ($in->post('is_billed') !== false) ? "是" : "否",
            'billing_date' => $in->post('billing_date'),
            'billing_amount' => $in->post('billing_amount'),
            'drawer' => $in->post('drawer'),
            'confirmor' => $in->post('confirmor'),
            'checkoff_date' => $in->post('checkoff_date'),
            
            /* 动态信息 */
            'tm_name' => $in->post('tm_name'),
            'tm_class' => $in->post('tm_class'),
            'application_date' => $in->post('application_date'),
            'application_no' => $in->post('application_no'),
            'result' => $in->post('result'),
        );
        
        /* mixture */
        $mixture = array();
        $view = $this->_get_dynamic_view($in->post('case_type'));
        switch ($view) {
            case 'application':
                $mixture['ap_sample_name'] = $in->post('ap_sample_name');
                $mixture['ap_sample_path'] = $in->post('ap_sample_path');
                $mixture['ap_trademark_type'] = $in->post('ap_trademark_type');
                $mixture['ap_color'] = $in->post('ap_color');
                $mixture['ap_item'] = $in->post('ap_item');
                $mixture['ap_announcement_date'] = $in->post('ap_announcement_date');
                $mixture['ap_registration_date'] = $in->post('ap_registration_date');
                $mixture['ap_renewal_date'] = $in->post('ap_renewal_date');
                break;
            case 'transfer':
                $mixture['tr_finish_date'] = $in->post('tr_finish_date');
                $mixture['tr_prev_name'] = $in->post('tr_prev_name');
                $mixture['tr_prev_addr'] = $in->post('tr_prev_addr');
                break;
            case 'change':
                $mixture['ch_finish_date'] = $in->post('ch_finish_date');
                $mixture['ch_prev_name'] = $in->post('ch_prev_name');
                $mixture['ch_prev_addr'] = $in->post('ch_prev_addr');
                break;
            case 'renewal':
                $mixture['re_finish_date'] = $in->post('re_finish_date');
                $mixture['re_to_date'] = $in->post('re_to_date');
                break;
            case 'other':
                $mixture['ot_finish_date'] = $in->post('ot_finish_date');
                $mixture['ot_remark'] = $in->post('ot_remark');
                break;
            case 'case':
                $mixture['ca_first_apply_date'] = $in->post('ca_first_apply_date');
                $mixture['ca_next_apply_date'] = $in->post('ca_next_apply_date');
                $mixture['ca_writer'] = $in->post('ca_writer');
                $mixture['ca_remark'] = $in->post('ca_remark');
                break;
        }
        $trademark['mixture'] = serialize($mixture);
        
        return $trademark;
    }
    
    /**
     * 从表单取限时提醒
     * @return array
     */
    private function _get_form_reminders() {
        $in = $this->input;

        if ($in->post('rm_filename') == FALSE) {
            return array();
        }
        
        $rm_filenames           = $in->post('rm_filename');
        $rm_receive_first_dates = $in->post('rm_receive_first_date');
        $rm_receive_last_dates  = $in->post('rm_receive_last_date');
        $rm_operate_dates       = $in->post('rm_operate_date');
        $rm_operate_results     = $in->post('rm_operate_result');
        $rm_operators           = $in->post('rm_operator');
        
        $reminders = array();
        $count = count($in->post('rm_filename'));
        for ($i = 0; $i < $count; $i++) {
            $reminders[] = array(
                'rm_filename'           => $rm_filenames[$i],
                'rm_receive_first_date' => $rm_receive_first_dates[$i],
                'rm_receive_last_date'  => $rm_receive_last_dates[$i],
                'rm_operate_date'       => $rm_operate_dates[$i],
                'rm_operate_result'     => $rm_operate_results[$i],
                'rm_operator'           => $rm_operators[$i],
            );
        }
        
        return $reminders;
    }
    
    /**
     * 从表单取官方文件
     */
    private function _get_form_documents() {
        $in = $this->input;

        if ($in->post('doc_name') == FALSE) {
            return array();
        }
        
        $doc_names         = $in->post('doc_name');
        $doc_entry_dates   = $in->post('doc_entry_date');
        $doc_paths         = $in->post('doc_path');
        $doc_original_name = $in->post('doc_original_name');
        
        $documents = array();
        $count = count($in->post('doc_name'));
        for ($i = 0; $i < $count; $i++) {
            $documents[] = array(
                'doc_name'          => $doc_names[$i],
                'doc_entry_date'    => $doc_entry_dates[$i],
                'doc_path'          => $doc_paths[$i],
                'doc_original_name' => $doc_original_name[$i],
            );
        }
        
        return $documents;
    }
    
    /**
     * 返回 案件类型 下拉列表的options
     * @return type
     */
    private function _case_type_options() {
        $options = array();
        foreach ($this->trademark_model->case_types() as $type) {
            $options[$type] = $type;
        }
        return $options;
    }
    
    private function _client_options() {
        $options = array(0 => '');
        foreach ($this->client_model->fetch_names() as $row) {
            $options[$row['id']] = htmlspecialchars($row['name']);
        }
        
        return $options;
    }
    
    private function _agency_options() {
        $options = array('' => '');
        foreach ($this->trademark_model->agencies() as $agency) {
            $options[$agency] = $agency;
        }
        return $options;
    }
    
    /**
     * 活跃人员options
     * @return type
     */
    private function _active_user_options() {
        $result = array('' => '');

        $conditions = array('active' => true);
        $users = $this->user_model->fetch_array($conditions);
        foreach ($users as $user) {
            $result[$user['u_id']] = htmlspecialchars($user['u_real_name']);
        }

        return $result;
    }
        
    /**
     * 所有人员姓名options
     * @return type
     */
    private function _user_realname_options() {
        $result = array('' => '');

        $users = $this->user_model->fetch_array();
        foreach ($users as $user) {
            $name = htmlspecialchars($user['u_real_name']);
            $result[$name] = $name;
        }

        return $result;
    }
    
    private function _partner_options() {
        $options = array('' => '');
        foreach ($this->partner_model->fetch_abbrnames() as $row) {
            $options[$row['id']] = htmlspecialchars($row['name']);
        }
        
        return $options;
    }
    
    private function _partner_name_options() {
        $options = array('' => '');
        foreach ($this->partner_model->fetch_abbrnames() as $row) {
            $name = htmlspecialchars($row['name']);
            $options[$name] = $name;
        }
        
        return $options;
    }
    
    /**
     * 财务人员options
     */
    private function _financial_user_options() {
        $result = array('' => '');

        $financial_uids = $this->user_role_model->get_role_uids(ROLE_FINANCE);
        $users = $this->user_model->fetch_financial_users($financial_uids);
        foreach ($users as $user) {
            $result[$user['u_id']] = htmlspecialchars($user['u_real_name']);
        }

        return $result;
    }

    /**
     * 设置表单验证规则
     */
    private function _set_form_validation_rules() {
        $fv = $this->form_validation;
        
        /* 基本信息 */
        $fv->set_rules('file_no', '', 'trim');
        $fv->set_rules('file_create_date', '建档日期', 'required');
        $fv->set_rules('case_type', '案件类型', 'required');
        $fv->set_rules('c_name', '申请人名称（中文）', 'required');
        $fv->set_rules('c_name_en', '', 'trim');
        $fv->set_rules('c_addr', '申请人地址（中文）', 'trim');
        $fv->set_rules('c_addr_en', '', 'trim');
        $fv->set_rules('c_licence_number', '', 'trim');
        $fv->set_rules('c_country', '', 'trim');
        $fv->set_rules('other_client_info', '', 'trim');
        $fv->set_rules('agency', '代理公司', 'required');
        $fv->set_rules('agent', '代理人', 'required');
        $fv->set_rules('partner', '合作所', 'trim');
        $fv->set_rules('ct_contact', '联系人', 'trim|required');
        $fv->set_rules('ct_telephone', '电话', 'trim|required');
        $fv->set_rules('ct_fax', '', 'trim');
        $fv->set_rules('ct_email', '', 'trim');
        $fv->set_rules('ct_mailing_addr', '邮寄地址', 'trim|required');
        $fv->set_rules('ct_qq', '', 'trim');

        /* 费用统计 */
        $fv->set_rules('government_fee', '官费', 'required|numeric');
        $fv->set_rules('agency_fee', '代理费', 'required|numeric');
        $fv->set_rules('total_fee', '总费用', 'trim');

        $fv->set_rules('is_collected', '是否收款', 'trim');
        $ext = ($this->input->post('is_collected') !== false) ? '|required' : '';
        $fv->set_rules('collection_date', '收款日期', 'trim' . $ext);
        $fv->set_rules('collection_account', '收款账户', 'trim' . $ext);
        
        $fv->set_rules('is_billed', '是否开票', 'trim');
        $ext = ($this->input->post('is_billed') !== false) ? '|required' : '';
        $fv->set_rules('billing_date', '开票日期', 'trim' . $ext);
        $fv->set_rules('billing_amount', '开票金额', 'numeric' . $ext);
        $fv->set_rules('drawer', '开票人', 'trim' . $ext);
        
        $fv->set_rules('confirmor', '', 'trim');
        $fv->set_rules('checkoff_date', '', 'trim');
        
        /* 动态页面 */
        $view = $this->_get_dynamic_view($this->input->post('case_type'));

        $fv->set_rules('tm_name', '商标名称', 'trim|required');
        $fv->set_rules('tm_class', '类别', 'trim|required');
        $fv->set_rules('application_date', '申请日期', 'trim');
        $fv->set_rules('application_no', '申请号', ($view == 'application') ? 'trim' : 'trim|required');
        $fv->set_rules('result', '结果', 'trim');
        
        switch ($view) {
            case 'application':
                $fv->set_rules('ap_sample_name', '标样', 'trim|required');
                $fv->set_rules('ap_sample_path', '', 'trim');
                $fv->set_rules('ap_trademark_type', '', 'trim');
                $fv->set_rules('ap_color', '', 'trim');
                $fv->set_rules('ap_item', '指定商品', 'trim|required');
                $fv->set_rules('ap_announcement_date', '', 'trim');
                $fv->set_rules('ap_registration_date', '', 'trim');
                $fv->set_rules('ap_renewal_date', '', 'trim');
                break;
            case 'transfer':
                $fv->set_rules('tr_finish_date', '', 'trim');
                $fv->set_rules('tr_prev_name', '转让人名称', 'trim|required');
                $fv->set_rules('tr_prev_addr', '转让人地址', 'trim|required');
                break;
            case 'change':
                $fv->set_rules('ch_finish_date', '', 'trim');
                $fv->set_rules('ch_prev_name', '变更人名称', 'trim');
                $fv->set_rules('ch_prev_addr', '变更人地址', 'trim');
                break;
            case 'renewal':
                $fv->set_rules('re_finish_date', '', 'trim');
                $fv->set_rules('re_to_date', '', 'trim');
                break;
            case 'other':
                $fv->set_rules('ot_finish_date', '', 'trim');
                $fv->set_rules('ot_remark', '', 'trim');
                break;
            case 'case':
                $fv->set_rules('ca_first_apply_date', '', 'trim');
                $fv->set_rules('ca_next_apply_date', '', 'trim');
                $fv->set_rules('ca_writer', '', 'trim');
                $fv->set_rules('ca_remark', '', 'trim');
                break;
        }
        
        /* 限时提醒 */
        if ($this->input->post('rm_filename') !== FALSE) {
            $fv->set_rules('rm_filename[]', '限时提醒 - 文件名称', 'required');
            $fv->set_rules('rm_receive_first_date[]', '收文日期', 'required');
            $fv->set_rules('rm_receive_last_date[]', '截止日期', 'required');
        }
        
        /* 官方文件 */
        $fv->set_rules('doc_path[]', '官方文件 - 附件', 'trim');
    }
}